package com.goodving.guli.fragment;

import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.goodving.guli.R;
import com.goodving.guli.bt.C0668a;
import com.goodving.guli.bt.Utils;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.bt.ui.C1795p;
import com.goodving.guli.server.WebAPI;
import com.goodving.guli.server.WebResponse;
import com.goodving.guli.utility.gulihardware.Constant;
import com.goodving.guli.view.C3247b;
import com.goodving.guli.view.C4220a;
import com.loopj.android.http.AsyncHttpResponseHandler;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import cn.com.smartdevices.bracelet.chart.util.ChartData;
import cn.com.smartdevices.bracelet.config.C0886a;
import cn.com.smartdevices.bracelet.config.Config;
import cz.msebera.android.httpclient.Header;

/**
 * Created by john on 2015/12/25.
 */
public class dF extends C1795p implements View.OnClickListener {
    private static final String f9852b = "SettingAboutActivity";
    private Context f9853c;
    private View f9854d;
    private View f9855e;
    private View f9856f;
    private TextView f9857g;
    private final int f9858h;
    private final int f9859i;
    private final int f9860j;
    private ProgressDialog f9861k;
    private Config f9862l;
    private Handler f9863m;
    private boolean f9864n;
    private View f9865o;

    class dG extends Handler {
        final /* synthetic */ dF f9866a;

        dG(dF dFVar) {
            this.f9866a = dFVar;
        }

        public void handleMessage(Message message) {
            switch (message.what) {
                case 1 /*1*/:
                    if (this.f9866a.f9861k == null) {
                        this.f9866a.f9861k = ProgressDialog.show(this.f9866a.getActivity(), this.f9866a.getString(R.string.upload_log), this.f9866a.getString(R.string.prepare_log));
                        this.f9866a.f9861k.setCancelable(true);
                    }
                case C4220a.f20888k /*2*/:
                    if (this.f9866a.f9861k != null) {
                        this.f9866a.f9861k.setMessage(this.f9866a.getString(R.string.uploaded) + message.arg1 + "%");
                    }
                case C4220a.f20889l /*3*/:
                    if (this.f9866a.f9861k != null) {
                        this.f9866a.f9861k.dismiss();
                        this.f9866a.f9861k = null;
                    }
                    if (this.f9866a.getActivity() == null) {
                        return;
                    }
                    if (((Boolean) message.obj).booleanValue()) {
                        C3247b.m22928a(this.f9866a.getActivity(), (int) R.string.upload_log_ok, 0).show();
                    } else {
                        C3247b.m22928a(this.f9866a.getActivity(), (int) R.string.upload_log_fail, 0).show();
                    }
                default:
            }
        }
    }

    public dF() {
        this.f9853c = null;
        this.f9856f = null;
        this.f9858h = 1;
        this.f9859i = 2;
        this.f9860j = 3;
        this.f9861k = null;
        this.f9862l = Config.m8876h();
        this.f9863m = new dG(this);
    }

    class dL implements Runnable {
        final /* synthetic */ Context f9871a;
        final /* synthetic */ dF f9872b;

        dL(dF dFVar, Context context) {
            this.f9872b = dFVar;
            this.f9871a = context;
        }

        public void run() {
            this.f9872b.m14599b(this.f9871a);
        }
    }

    private void m14596a(Context context) {
        new Thread(new dL(this, context)).start();
    }

    class dH implements View.OnLongClickListener {
        final /* synthetic */ dF f9867a;

        dH(dF dFVar) {
            this.f9867a = dFVar;
        }

        public boolean onLongClick(View view) {
            this.f9867a.m14606e();
            return true;
        }
    }

    class dI implements View.OnLongClickListener {
        final /* synthetic */ dF f9868a;

        dI(dF dFVar) {
            this.f9868a = dFVar;
        }

        public boolean onLongClick(View view) {
            return true;
        }
    }

    private void m14597a(View view) {
        this.f9854d = view.findViewById(R.id.setting_about_user_agreement);
        if (Utils.m8083k()) {
            ((TextView) view.findViewById(R.id.user_agreement_txt)).setText(getString(R.string.user_agreement_play));
        }
        this.f9855e = view.findViewById(R.id.setting_about_website);
        this.f9854d.setOnClickListener(this);
        this.f9855e.setOnClickListener(this);
        View findViewById = view.findViewById(R.id.setting_check_apk_upgrade);
        findViewById.setOnClickListener(this);
        View findViewById2 = view.findViewById(R.id.buy_bracelet_stuff);
        findViewById2.setOnClickListener(this);
        if (this.f9862l.f4204n.f4257a.booleanValue()) {
            this.f9865o = view.findViewById(R.id.setting_check_apk_upgrade_new_icon);
            if (this.f9864n) {
                this.f9865o.setVisibility(View.VISIBLE);
            }
            findViewById.setVisibility(View.VISIBLE);
            view.findViewById(R.id.split_line_about_apk_upgrade).setVisibility(View.VISIBLE);
        } else {
            findViewById.setVisibility(View.GONE);
            view.findViewById(R.id.split_line_about_apk_upgrade).setVisibility(View.GONE);
        }
        if (this.f9862l.f4198h.f4242a.booleanValue()) {
            findViewById2.setVisibility(View.VISIBLE);
            view.findViewById(R.id.split_line_about_buy_band).setVisibility(View.VISIBLE);
        } else {
            findViewById2.setVisibility(View.GONE);
            view.findViewById(R.id.split_line_about_buy_band).setVisibility(View.GONE);
        }
        this.f9857g = (TextView) view.findViewById(R.id.setting_about_version_value);
        view.findViewById(R.id.setting_feedback).setOnClickListener(this);
        view.findViewById(R.id.setting_feedback).setOnLongClickListener(new dH(this));
        this.f9856f = view.findViewById(R.id.logo);
        this.f9856f.setOnClickListener(this);
        this.f9856f.setOnLongClickListener(new dI(this));
        view.findViewById(R.id.enable_hidden_feature).setOnClickListener(this);
    }

    class dM extends AsyncHttpResponseHandler {
        final /* synthetic */ Context f9873a;
        final /* synthetic */ File f9874b;
        final /* synthetic */ dF f9875c;

        dM(dF dFVar, Context context, File file) {
            this.f9875c = dFVar;
            this.f9873a = context;
            this.f9874b = file;
        }

        public void onFailure(int i, Header[] headerArr, byte[] bArr, Throwable th) {
            GuliLog.tracePrintInfo("WPJ", "onFailure");
            if (this.f9875c.f9863m != null) {
                Message message = new Message();
                message.what = 3;
                message.obj = Boolean.valueOf(false);
                this.f9875c.f9863m.sendMessage(message);
                if (this.f9874b != null && this.f9874b.exists()) {
                    this.f9874b.delete();
                }
            }
        }

        public void onProgress(int i, int i2) {
            if (this.f9875c.f9863m != null) {
                Message message = new Message();
                message.what = 2;
                message.arg1 = (i * 100) / i2;
                this.f9875c.f9863m.sendMessage(message);
            }
        }

        public void onSuccess(int i, Header[] headerArr, byte[] bArr) {
            GuliLog.tracePrintInfo("WPJ", "onSuccess:" + new String(bArr));
            if (this.f9875c.f9863m != null) {
                Message message = new Message();
                message.what = 3;
                if (bArr == null || !WebResponse.getWebResponse(this.f9873a, new String(bArr)).m11659c()) {
                    message.obj = Boolean.valueOf(false);
                } else {
                    message.obj = Boolean.valueOf(true);
                }
                this.f9875c.f9863m.sendMessage(message);
                if (this.f9874b != null && this.f9874b.exists()) {
                    this.f9874b.delete();
                }
            }
        }
    }

    private void m14599b(Context context) {
        try {
            if (this.f9863m != null) {
                this.f9863m.sendEmptyMessage(1);
            }
            String format = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
            String absolutePath = Environment.getExternalStorageDirectory().getAbsolutePath();
            File file = new File(absolutePath + "/mili_log_" + format + ".zip");
            if (file.exists()) {
                file.delete();
            }
            byte[] bArr = new byte[ChartData.f4049d];
            File file2 = new File(absolutePath + "/mili_log.txt");
            FileInputStream fileInputStream = new FileInputStream(file2);
            long length = file2.length() - 52428800;
            while (length > 0) {
                long skip = fileInputStream.skip(length);
                if (skip == -1) {
                    break;
                }
                length -= skip;
            }
            ZipEntry zipEntry = new ZipEntry("mili_log_" + format + ".txt");
            ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(file));
            zipOutputStream.putNextEntry(zipEntry);
            while (true) {
                int read = fileInputStream.read(bArr, 0, bArr.length);
                if (read > 0) {
                    zipOutputStream.write(bArr, 0, read);
                } else {
                    zipOutputStream.closeEntry();
                    fileInputStream.close();
                    zipOutputStream.close();
                    WebAPI.uploadLogFileBlock(context, C0886a.m9071f(context), file, new dM(this, context, file));
                    return;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void m14603c() {
        try {
            startActivity(new Intent(getActivity(), Class.forName("cn.com.smartdevices.ble.WeightTestAcitvity")));
        } catch (Exception e) {
        }
    }

    private void m14605d() {
        try {
            startActivity(new Intent(getActivity(), Class.forName("cn.com.smartdevices.bracelet.shoes.ui.ShoesBindTestActivity")));
        } catch (Exception e) {
        }
    }

    class dJ implements DialogInterface.OnClickListener {
        final /* synthetic */ dF f9869a;

        dJ(dF dFVar) {
            this.f9869a = dFVar;
        }

        public void onClick(DialogInterface dialogInterface, int i) {
            this.f9869a.m14596a(this.f9869a.f9853c.getApplicationContext());
            dialogInterface.dismiss();
        }
    }

    class dK implements DialogInterface.OnClickListener {
        final /* synthetic */ dF f9870a;

        dK(dF dFVar) {
            this.f9870a = dFVar;
        }

        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();
        }
    }

    private void m14606e() {
        Builder builder = new Builder(getActivity());
        builder.setTitle(R.string.upload_log);
        builder.setMessage(R.string.upload_log_info);
        builder.setPositiveButton(R.string.confirm, new dJ(this));
        builder.setNegativeButton(R.string.cancel, new dK(this));
        builder.show();
    }

    private void m14607f() {
//        if (Utils.m8083k()) {
//            startActivity(new Intent(getActivity(), SettingAboutUserAgreementActivity.class));
//            return;
//        }
//        Utils.m8096q(getActivity());
        C0668a.m8126a(getActivity(), C0668a.ef);
    }

    protected boolean m13559b () { // m14608b
        return true;
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.setting_feedback:
//                startActivity(new Intent(getActivity(), SettingFeedbackActivity.class));
//                C0668a.m8126a(getActivity(), C0668a.ed);
                break;
            case R.id.setting_about_website:
                String str = "http://www.mi.com/en/";
                String locale = Locale.getDefault().toString();
                if (locale.startsWith(Locale.ENGLISH.toString())) {
                    str = "http://www.mi.com/en/";
                } else if (Locale.SIMPLIFIED_CHINESE.toString().equals(locale)) {
                    str = "http://www.mi.com/";
                } else if (Locale.TRADITIONAL_CHINESE.toString().equals(locale)) {
                    str = "http://www.mi.com/tw/";
                } else if ("zh_HK".equals(locale)) {
                    str = "http://www.mi.com/hk/";
                }
                GuliLog.tracePrintInfo(f9852b, "Locale.getdefault =" + Locale.getDefault() + str);
                Intent intent = new Intent("android.intent.action.VIEW");
                intent.setData(Uri.parse(str));
                startActivity(intent);
                C0668a.m8126a(getActivity(), C0668a.ec);
                break;
            case R.id.setting_check_apk_upgrade:
                if (Utils.m8084k(getActivity())) {
                    if (this.f9864n) {
                        this.f9865o.setVisibility(View.GONE);
                    }
                    C3247b.m22928a(getActivity(), (int) R.string.checking_new_apk, 0).show();
//                    Utils.m7994a(getActivity(), true);
                } else {
                    C3247b.m22928a(getActivity(), (int) R.string.no_network_connection, 0).show();
                }
                C0668a.m8126a(getActivity(), C0668a.eb);
                break;
            case R.id.buy_bracelet_stuff:
//                C0891d.m9079a(getActivity(), true);
                C0668a.m8128a(getActivity(), C0668a.be, C0668a.ce);
                break;
            case R.id.setting_about_user_agreement:
                m14607f();
                break;
            default:
                break;
        }
    }

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        this.f9853c = getActivity();
        Bundle arguments = getArguments();
        if (arguments != null) {
            this.f9864n = arguments.getBoolean(Constant.aP, false);
        }
        GuliLog.tracePrintInfo(f9852b, "bundle = " + arguments + " isNew = " + this.f9864n);
    }

    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View inflate = layoutInflater.inflate(R.layout.fragment_setting_about, viewGroup, false);
        m14597a(inflate);
        return inflate;
    }

    public void onDestroy() {
        super.onDestroy();
        this.f9863m.removeCallbacksAndMessages(null);
        this.f9863m = null;
    }

    public void onPause() {
        super.onPause();
        C0668a.m8141b(C0668a.f3542L);
    }

    public void onResume() {
        super.onResume();
        GuliLog.tracePrintInfo(f9852b, "onResume() ");
        try {
            this.f9857g.setText(getActivity().getPackageManager().getPackageInfo(getActivity().getPackageName(), 0).versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        C0668a.m8133a(C0668a.f3542L);
    }
}
